/**
 * Created by Aoye on 2017/1/12.
 */
$(function () {
    $('#vImg').click(function () {
        $(this).attr('src','/door/front/parent/imageCode?t='+Math.random()+'width=120&height=40&codeLength=4&fontSize=30')
    });
	$("body").keydown(function (event) {
		if(event.keyCode == 13){
			$('#submit-btn').click();
		}
	})
    $('#submit-btn').click(function () {
    	var flag = isTrue();
    	if(!flag){
    		return false;
    	}
        var data={
            userName:$('#userName').val(),
            password:$('#password').val(),
            code:$('#code').val()
        };
        $.ajax({
            type:'POST',
            url:'/door/front/parent/check',
            // dataType:'json',
            async:true,
            data:data,
            success:function (data,textStatus) {
            	var srtJSON = JSON.parse(data);
                if (srtJSON.errorcode != 0) {
                	Tip(srtJSON.errormessage);
                	$(".login-form input").each(function(i){
                        if(i>0){
                            $(this).val("");
                        }
                    });
                    $(".login-form input").get(1).focus();
                }else{
                    window.location.href="card_pc.html";
                }
                $('#vImg').attr('src','/door/front/parent/imageCode?t='+Math.random()+'width=120&height=40&codeLength=4&fontSize=30');
            },
            error:function (err) {
                console.log(err);
                console.log(data.userName)
            }
        })
    })
});
function Tip(tip){
	$(".tips").text(tip);
	$(".tips").css("visibility", "visible");
	
}
function isTrue(){
	var value1 = $("input:eq(0)").val();
	var value2 = $("input:eq(1)").val();
	var value3 = $("input:eq(2)").val();
	if(value1==''){
		Tip("用户名不能为空");
		return false;
	}
	if(value2==''){
		Tip("密码不能为空");
		return false;
	}
	if(value3==''){
		Tip("验证码不能为空");
		return false;
	}
	$(".tips").css("visibility", "hidden");
	return true;
}
